home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1249 / permute.t < prev    next >
Text File  |  1997-04-18  |  1KB  |  82 lines

  1. %
  2. % "permute.t" shows all permutations of a sequence of
  3. % integers
  4. %
  5. %   Sample program for the T Interpreter by:
  6. %
  7. %   Stephen R. Schmitt
  8. %   962 Depot Road
  9. %   Boxborough, MA 01719
  10. %
  11.  
  12. const num : int := 4
  13. var a : array[num] of int
  14. var pos : int
  15.  
  16. program
  17.  
  18.     var i : int
  19.  
  20.     pos := -1
  21.  
  22.     for i := 1 ... num-1 do 
  23.  
  24.         a[i] := 0
  25.  
  26.     end for
  27.     
  28.     permute( 0 )
  29.  
  30. end program
  31.  
  32. %
  33. % arrange global array a in all possible ways
  34. %
  35. procedure permute( n : int )
  36.  
  37.     var i : int
  38.  
  39.     pos := pos + 1
  40.     a[n] := pos
  41.     
  42.     if pos = num-1 then 
  43.  
  44.         show_array
  45.  
  46.     end if
  47.    
  48.     if pos ~= num - 1 then
  49.  
  50.         for i := 0...num-1 do
  51.  
  52.             if a[i] = 0 then 
  53.  
  54.                 permute( i )
  55.  
  56.             end if
  57.  
  58.         end for
  59.  
  60.     end if
  61.    
  62.     pos := pos - 1
  63.     a[n] := 0
  64.  
  65. end procedure
  66.  
  67. %
  68. % display contents of global array a 
  69. %
  70. procedure show_array
  71.  
  72.     var i : int
  73.  
  74.     for i := 0 ... num-1 do 
  75.  
  76.         put a[i] : 3 ...
  77.  
  78.     end for
  79.  
  80.     put ""
  81.    
  82. end procedure